/*******************************************************************
 *
 * Copyright (C), 2021-2022, LS, All rights reserved.
 *
 * Author      : 
 * Create Time : 2021-09-17
 * Version     : v0.1
 * Description : 
 *******************************************************************/

/*********************************************************************
    INCLUDES
*/

#include "att.h"
#include "hci.h"
#include <string.h>
#include "OSAL.h"
#include "ll_common.h"
#include "bcomdef.h"
#include "peripheral.h"
#include "osal_app.h"
#include "log.h"

#include "sys_task.h"
#include "app_misc.h"
#include "app_time.h"
#include "app_reset.h"
#include "app_motor.h"
#include "app_button.h"
#include "app_battery.h"
#include "app_display.h"
#include "app_pwr_mgr.h"
#include "app_skip_alg.h"
#include "app_global_data.h"
#include "user_config.h"
#include "bsp_hw_config.h"

#include "ble_app_util.h"
#include "ls_ble_sdk_api.h"
#include "ls_ble_phy62xx.h"
#include "ls_ble_sdk_config.h"
#include "ls_ble_custom_protocol.h"

/*********************************************************************
    MACROS
*/

/*********************************************************************
    CONSTANTS
*/

/*********************************************************************
    TYPEDEFS
*/

/*********************************************************************
    GLOBAL VARIABLES
*/

/*********************************************************************
    EXTERNAL VARIABLES
*/

/*********************************************************************
    EXTERNAL FUNCTIONS
*/
extern void ui_tick_4hz_cnt_handle(void);

/*********************************************************************
    LOCAL VARIABLES
*/
uint8 osalApp_TaskID;   // Task ID for internal task/event processing
static bool g_revert = false;

/*********************************************************************
    LOCAL FUNCTIONS
*/

/*********************************************************************
    PROFILE CALLBACKS
*/

void app_delay_reset(bool revert)
{
    g_revert = revert;
    osal_start_timerEx(osalApp_TaskID, APP_DELAY_RESET_EVT, 500);
}


void osalAppInit(uint8 task_id)
{
    osalApp_TaskID = task_id;
    LOG("osalAppInit\n");

    app_pwr_mgr_init();
    app_peripheral_init();
    
    
    /*app_pwr_mgr_exit_sleep();
    app_disp_init();
    app_disp_backlight_set(true);
    
    //extern void app_disp_image_draw_test(uint16_t x, uint16_t y, uint16_t width, uint16_t height);
    //app_disp_image_draw_test(10, 10, 35, 35);
    
    extern void app_disp_img_test(void);
    app_disp_img_test();*/
    
    sys_task_msg_send(MSG_TYPE_UI, UI_EVT_POWERON_FULL_DISP);
}


uint16 osalAppProcEvt( uint8 task_id, uint16 events )
{
    VOID task_id; // OSAL required parameter that isn't used in this function

    if ( events & SYS_EVENT_MSG )
    {
        uint8* pMsg;
        if ( (pMsg = osal_msg_receive( osalApp_TaskID )) != NULL )
        {
            sys_task_handle((sys_evt_t*) pMsg);
            // Release the OSAL message
            VOID osal_msg_deallocate( pMsg );
        }
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
    }


    if ( events & APP_MOTOR_STOP_TIMER_EVT )
    {
        app_motor_stop();
        return ( events ^ APP_MOTOR_STOP_TIMER_EVT );
    }
    
    if ( events & APP_BUTTON_TIMER_EVT )
    {
        app_button_timer_detect_timeout_handle();
        return ( events ^ APP_BUTTON_TIMER_EVT );
    }
    
    if ( events & APP_BTN_CLICK_TIMEOUT_EVT )
    {
        app_button_click_timeout_handle();
        return ( events ^ APP_BTN_CLICK_TIMEOUT_EVT );
    }
    
    if ( events & APP_UI_TICK_TIMER_EVT )
    {
        ui_tick_4hz_cnt_handle();
        return ( events ^ APP_UI_TICK_TIMER_EVT );
    }
    
    if ( events & APP_SKIP_MAX_INV_TIMER_EVT )
    {
        app_skip_alg_max_inv_timeout_handle();
        return ( events ^ APP_SKIP_MAX_INV_TIMER_EVT );
    }


    if ( events & APP_BATT_ADC_TIMER_EVT )
    {
        app_batt_mea_timer_timeout_handler();
        return ( events ^ APP_BATT_ADC_TIMER_EVT );
    }
    
#if(BATT_CHARGE_ENABLE)
    if ( events & APP_BATT_CHG_CHK_TIMER_EVT )
    {
        app_batt_charge_status_check_handle();
        return ( events ^ APP_BATT_CHG_CHK_TIMER_EVT );
    }
#endif

    if( events & APP_DT_TICK_TIMER_EVT)
    {
        app_datetime_sync_handler();
        return ( events ^ APP_DT_TICK_TIMER_EVT );
    }


    if ( events & APP_DELAY_RESET_EVT )
    {
        if(g_revert) {
            sys_task_msg_send(MSG_TYPE_UI, UI_EVT_SHIP_MODE_RESET);
        }
        else {
            app_system_reset();
        }
        return ( events ^ APP_DELAY_RESET_EVT );
    }

    if( events & APP_TEST_EVT)
    {
        return ( events ^ APP_TEST_EVT );
    }

    return 0;
}


